<template>
  <div class="login">
    <h1>欢迎登录</h1>

    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" />
      </el-form-item>

      <el-form-item label="密码" prop="userpwd">
        <el-input v-model="ruleForm.userpwd" type="password" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { userLogin } from "@/service/userService";
import { useRouter } from "vue-router";
import coms from "@/components/index";
import { MenuStore } from "@/stores";
let router = useRouter();

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  username: "",
  userpwd: "",
});

const rules = reactive<FormRules>({
  username: [
    { required: true, message: "用户名不能为空", trigger: "blur" },
    { min: 3, max: 12, message: "长度只能是3到12位", trigger: "blur" },
  ],
  userpwd: [
    { required: true, message: "密码不能为空", trigger: "change" },
    {
      pattern: /^[0-9a-z]{6,10}$/,
      message: "密码只能数字字母组成长度6到10位",
      trigger: "change",
    },
  ],
});
//提交
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const { data } = await userLogin({
        username: ruleForm.username,
        userpwd: ruleForm.userpwd,
      });
      if (data.code == 200) {
        localStorage.setItem("token", data.token);
        const menu = data.menu;
        menu.forEach((item: any) => {
          router.addRoute("main", {
            name: item.path,
            path: item.path,
            component: coms[item.component],
          });
        });
        MenuStore().menuListInit(menu);
        window.localStorage.setItem("menu", JSON.stringify(menu));
        //把用户登录的路由信息  放入本地存储
        // router.push("/main");  //需要设置默认的子路由
        router.push("/main/" + menu[0].path); //默认跳转到第一子路由

        // router.addRoute("main", {
        //   name: "defaultmain",
        //   path: "",
        //   component: coms[menu.component],
        // });

        router.push("/main");
      } else {
      }
    } else {
    }
  });
};
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
</script>

<style lang="scss" scoped>
.login {
  width: 40%;
  height: 40%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
